import http from "@/axios";
import type {BaseResponse, PageBaseResponse} from "@/api/baseResponse";
import type {PageBaseRequest} from "@/api/baseRequest";

enum ApiUrl {
    loginUrl = "/mqtt/user/login",
    logoutUrl = "/mqtt/user/logout",
    pageUserUrl = "/mqtt/user/page",
    createUserUrl = "/mqtt/user/create",
    deleteUserUrl = "/mqtt/user/delete"
}

export interface LoginRequest {
    username: string;
    password: string;
}

export interface UserResponse {
    id: number;
    username: string;
    isAdmin: number;
    createDatetime: string;
}

export interface PageUserRequest extends PageBaseRequest {
    username?: number;
    isAdmin?: number;
}

export interface UserRequest {
    username?: string;
    password?: string;
    isAdmin?: number;
}

export function userLogin(data: LoginRequest) {
    return http.post<BaseResponse<string>>(ApiUrl.loginUrl, data)
}

export function userLogout() {
    return http.post<BaseResponse<boolean>>(ApiUrl.logoutUrl)
}

export function pageUser(data: PageUserRequest) {
    return http.post<PageBaseResponse<UserResponse[]>>(ApiUrl.pageUserUrl, data)
}

export function createUser(data: UserRequest) {
    return http.post<BaseResponse<boolean>>(ApiUrl.createUserUrl, data)
}

export function deleteUser(id: number) {
    return http.delete<BaseResponse<boolean>>(ApiUrl.deleteUserUrl, {
        params: {
            id: id
        }
    })
}